<?php

namespace Biz\service\advertisement\apiModule;


use Biz\model\brand\Brand as mBrand;
use Biz\service\brand\Brand;
use Biz\service\wiki\Wiki;
use Doraemon\model\Member;
use Doraemon\model\Position\PositionBaseInfo;
use Doraemon\pockets\IdSender\IdSender;
use Doraemon\service\Article;
use Doraemon\service\Blank;
use Doraemon\service\Format\Note\HandleNote;
use Doraemon\service\Guide;
use Doraemon\service\Ideabook;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\service\TopicActivity;
use Doraemon\service\User;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;
use Doraemon\tools\LinkFormat;

class Tab4Card extends BaseModule
{
    const FORMAT_HIT = 'formatPosHit';

    const EXT_TYPE_FLOW = 1;
    const EXT_TYPE_KA = 3;
    const EXT_MAP = [
        self::EXT_TYPE_FLOW => 'Banner卡片',
        self::EXT_TYPE_KA => '原生内容(无广告词)',
    ];

    /**
     * 推荐线api格式化输出
     * @param $hits
     * @param $uid
     * @param $userProfileExt
     * @param $statSignExt
     * @return array
     */
    public function format($hits, $uid, $userProfileExt, $statSignExt)
    {
        $response = [];

        if ($hits) {
            foreach ($hits as $hit) {
                //公共信息赋值
                $index = intval($hit['pos']);
                $adm_comment = (string)$hit['remark'];
                $reason = $hit['reason'];
                $type = null;
                $map = [
                    PositionBaseInfo::REASON_TYPE_BUSINESS => 'biz_ad',
                    PositionBaseInfo::REASON_TYPE_KA => 'disigner_ad',
                    PositionBaseInfo::REASON_TYPE_OPERATE => 'op_suggest',
                    PositionBaseInfo::REASON_TYPE_NEW_USER => 'welcome_new_user',
                ];
                $params = [
                    'suggestsign' => $map[$reason] ?? '',
                    'pos_id' => $hit['id'],
                    'page' => 0,
                ];
                if ($hit['ext_type'] == self::EXT_TYPE_KA) {
                    //KA广告
                    $extentdInfo = $hit['ext'];
                    $obj_id = $extentdInfo['obj_id'] ?? '';
                    $type = $extentdInfo['obj_type'] ?? null;
                    $posi = 'recommendPage';

                    if (!$obj_id) {
                        continue;
                    }
                    $type = ($type == BlockType::TYPE_WIKI) ? $type : (isset($type) ? $type : IdSender::getType($obj_id));
                    $tempData = [];
                    switch ($type) {
                        case IdSender::TYPE_WIKI_ALBUM:
                            $album_list = \Biz\service\Album\AlbumListService::getAlbumListByAlbumIds([$obj_id]);
                            $map = [];
                            foreach ($album_list as $info) {
                                if (isset($info['album_info'])) $map[$info['album_info']['album_id']] = $info;
                            }
                            if (isset($map[$obj_id])) {
                                $tempData = $map[$obj_id];
                            }
                            break;
                        case BlockType::TYPE_IDEABOOK:
                            $ideaBook = Ideabook::listIdeaBookInfo([$obj_id]);
                            if ($ideaBook && isset($ideaBook[$obj_id])) {
                                $tempData = [
                                    'folder' => isset($ideaBook[$obj_id]) ? $ideaBook[$obj_id] : []
                                ];
                            }
                            break;
                        case BlockType::TYPE_TOPIC_ACTIVITY:
                            $topic = TopicActivity::getTopicInfosForList([$obj_id]);
                            $tempData = isset($topic[$obj_id]) ? $topic[$obj_id] : [];
                            break;
                        case IdSender::TYPE_PHOTO:
                            $type = BlockType::TYPE_PHOTO;
                            //$photo_list = Photos::getPhotosForDynamic([$obj_id], $uid);
                            $photo_list = HandleNote::handleNote([$obj_id], HandleNote::WIKI_MASONRY_LAYOUTS, $uid);
                            if ($photo_list && isset($photo_list[$obj_id])) {
                                $photo = $photo_list[$obj_id]['photo'];
                                if ($photo['wiki_info']['list'] && is_array($photo['wiki_info']['list'])) {
                                    $photo['wiki_info']['style'] = Wiki::WIKI_STYLE_GOOD_THING;
                                    $photo['wiki_info']['list'] = \Doraemon\tools\StatSign::versionCompatible($photo['wiki_info']['list'], \Doraemon\tools\StatSign::WIKI_UP_GRADE_VERSION_522, BlockType::TYPE_WIKI, 'wiki');
                                }
                                $tempData = [
                                    'photo' => $photo,
                                    'action_info' => ['type' => 11, 'addtime' => $photo_list[$obj_id]['photo_info']['addtime']],
                                ];
                            }
                            break;
                        case IdSender::TYPE_GUIDE:
                            $type = BlockType::TYPE_GUIDE;
                            $guide_list = Guide::getGuidesForDynamic([$obj_id], $uid);
                            if ($guide_list && isset($guide_list[$obj_id])) {
                                $tempData = [
                                    'guide' => $guide_list[$obj_id],
                                    'action_info' => ['type' => 11, 'addtime' => $guide_list[$obj_id]['guide_info']['addtime']],
                                ];
                            }
                            break;
                        case IdSender::TYPE_ANSWER:
                            $type = BlockType::TYPE_PHOTO;
                            //$photo_list = Photos::getPhotosForDynamic([$obj_id], $uid);
                            $photo_list = HandleNote::handleNote([$obj_id], HandleNote::WIKI_MASONRY_LAYOUTS, $uid);
                            if ($photo_list && isset($photo_list[$obj_id])) {
                                $photo = $photo_list[$obj_id]['photo'];
                                if ($photo['wiki_info']['list'] && is_array($photo['wiki_info']['list'])) {
                                    $photo['wiki_info']['style'] = Wiki::WIKI_STYLE_GOOD_THING;
                                    $photo['wiki_info']['list'] = \Doraemon\tools\StatSign::versionCompatible($photo['wiki_info']['list'], \Doraemon\tools\StatSign::WIKI_UP_GRADE_VERSION_522, BlockType::TYPE_WIKI, 'wiki');
                                }
                                $tempData = [
                                    'photo' => $photo,
                                    'action_info' => ['type' => 11, 'addtime' => $photo_list[$obj_id]['photo_info']['addtime']],
                                ];
                            }
                            break;
                        case IdSender::TYPE_ARTICLE:
                            $type = BlockType::TYPE_ARTICLE;
                            $article_list = Article::getArticlesForDynamic([$obj_id], $uid);
                            if ($article_list && isset($article_list[$obj_id])) {
                                $tempData = [
                                    'article' => $article_list[$obj_id],
                                    'action_info' => ['type' => 11, 'addtime' => $article_list[$obj_id]['article_info']['addtime']],
                                ];
                            }
                            break;
                        case IdSender::TYPE_BLANK:
                            $type = BlockType::TYPE_BLANK;
                            $blank_list = Blank::getBlanksForDynamic([$obj_id], $uid);
                            if ($blank_list && isset($blank_list[$obj_id])) {
                                $tempData = [
                                    'blank' => $blank_list[$obj_id],
                                ];
                            }
                            break;
                        case BlockType::TYPE_WIKI:
                            $type = BlockType::TYPE_WIKI;
                            $wiki_list = Wiki::getWikiList([$obj_id], $uid);
                            if ($wiki_list && isset($wiki_list[$obj_id])) {
                                if (version_compare(\Doraemon\tools\HhzClient::getClientVersion(), \Doraemon\tools\StatSign::WIKI_UP_GRADE_VERSION_522, ">=")) {
                                    $tempData = [
                                        'wiki' => $wiki_list[$obj_id],
                                    ];
                                } else {
                                    $tempData = [
                                        'wiki' => ['wiki_info' => $wiki_list[$obj_id]],
                                    ];
                                }
                            }
                            break;
                        case BlockType::TYPE_USER_LIST:
                            $userInfo = User::getBasicUserInfo($obj_id);
                            if ($userInfo['type'] == Member::AUTH_USER_BRAND) {
                                $type = BlockType::TYPE_BRAND;
                                $brandId = mBrand::getBrandIdByUid($obj_id);
                                $brandList = Brand::multiFormatBrandCard([$brandId]);
                                if ($brandList) {
                                    $tempData = [
                                        'brand' => $brandList[$brandId]
                                    ];
                                }
                            }
                            break;
                        default:
                            break;
                    }

                    if ($tempData) {
                        $extend = [
                            basicStatSign::EXTEND_KEY_OBJ_ID => $obj_id,
                            basicStatSign::EXTEND_KEY_OBJ_TYPE => $type,
                            basicStatSign::EXTEND_KEY_INDEX => $index,
                            basicStatSign::EXTEND_KEY_CONFIG_ID => $hit['id'],
                        ];
                        $statSign = basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_FEED_CARD, $extend);
                        $tempData = [
                                'index' => $index,
                                'is_ad' => 1,
                                'type' => $type,
                                'obj_id' => $obj_id,
                                'statSign' => $statSign
                            ] + $tempData;
                    }
                } else {
                    $link = LinkFormat::generateLink($hit['link_type'], $hit['link_params']);
                    $link = $link['app_template'];
                    $extend = [
                        basicStatSign::EXTEND_KEY_OBJ_ID => $hit['id'],
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_BANNER,
                        basicStatSign::EXTEND_KEY_INDEX => $index,
                        basicStatSign::EXTEND_KEY_CONFIG_ID => $hit['id'],
                        basicStatSign::EXTEND_KEY_URL => $link,
                    ];
                    $statSign = basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_FEED_CARD, $extend);

                    $tempData = [
                        'type' => BlockType::TYPE_BANNER,
                        'obj_id' => $hit['id'],
                        'banner' => [
                            'id' => $hit['id'],
                            'index' => $index,
                            'banner' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'link' => $link,
                            'statSign' => $statSign
                        ],
                        'index' => $index,
                    ];

                    if (version_compare(HhzClient::getClientVersion(), \Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, '>=')) {
                        $tempData['statSign'] = $tempData['banner']['statSign'];
                        unset($tempData['banner']['statSign']);
                    }
                }

                if ($tempData) {
                    $response[] = $tempData;
                }
            }
        }
        return $response;
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        $extInfo = empty($map[$v['id']]) ? [] : $map[$v['id']];
        $v['obj_id'] = $extInfo['obj_id'] ?? '';
        $v['obj_type'] = $extInfo['obj_type'] ?? '';
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }
}